package com.gzhryc.html.models;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.gzhryc.html.dao.db.Website;
import com.gzhryc.html.dao.db.WebsitePage;
import com.gzhryc.html.dao.enums.WebsiteEnum;

public class WebsiteData {
	
	private ReadWriteLock rwl = new ReentrantReadWriteLock();
	
	private Lock r = rwl.readLock();
	private Lock w = rwl.writeLock();

	private Website website;
	private ConcurrentHashMap<String,WebsitePageWrap> pages;
	private boolean isFullPathPrefix;		//是否是全路径前缀，"/"代表全路径覆盖
	private boolean isAutoType;				//是否是自动类型
	
	public WebsiteData(Website website) {
		this.website = website;
		if(StringUtils.isBlank(this.website.getPathPrefix()) || this.website.getPathPrefix().equals("/")) {
			this.isFullPathPrefix = true;
		}else {
			this.isFullPathPrefix = false;
		}
		if(WebsiteEnum.Type.Auto.index().equals(this.website.getType())) {
			this.isAutoType = true;
		}else {
			this.isAutoType = false;
		}
	}
	
	public WebsitePage getPage(String path) {
		WebsitePageWrap wrap =  pages.get(path);
		if(wrap != null) {
			return wrap.getPage();
		}
		return null;
	}
	
	public void setPage(WebsitePage page) {
		if(page != null) {
			pages.put(page.getPath(), new WebsitePageWrap(page));
		}
	}
	
	/**
	 * 移除不只列表中的路径页面
	 * @param pathList
	 */
	public void removeNotContains(List<String> pathList) {
		Enumeration<String> enumeration = pages.keys();
		List<String> temp = new ArrayList<String>();
		while(enumeration.hasMoreElements()) {
			String key = enumeration.nextElement();
			if(!pathList.contains(key)) {
				temp.add(key);
			}
		}
		for(String key : temp) {
			pages.remove(key);
		}
	}

	public List<WebsitePage> getPages() {
		List<WebsitePage> result = new ArrayList<WebsitePage>();
		List<WebsitePageWrap> data = new ArrayList<WebsitePageWrap>(pages.values());
		for(WebsitePageWrap item : data) {
			result.add(item.getPage());
		}
		return result;
	}


	public void setPages(List<WebsitePage> pages) {
		this.pages = new ConcurrentHashMap<String,WebsitePageWrap>();  //重新初始化
		if(pages != null) {
			for(WebsitePage page : pages) {
				if(StringUtils.isBlank(page.getTitle())) {
					page.setTitle(website.getTitle());
				}
				if(StringUtils.isBlank(page.getKeywords())) {
					page.setKeywords(website.getKeywords());
				}
				if(StringUtils.isBlank(page.getDescription())) {
					page.setDescription(website.getDescription());
				}
				if(StringUtils.isBlank(page.getHeadHtml())) {
					page.setHeadHtml(website.getHeadHtml());
				}
				if(StringUtils.isBlank(page.getFootHtml())) {
					page.setFootHtml(website.getFootHtml());
				}
			}
			for(WebsitePage page : pages) {
				setPage(page);
			}
		}
	}

	public void setWebsite(Website website) {
		try {
            w.lock();
            this.website = website;
		} finally {
            w.unlock();
        }
	}

	public Website getWebsite() {
		try {
            r.lock();
            return website;
		} finally {
            r.unlock();
        }
	}
	
	/**
	 * 匹配访问路径前缀
	 * @param url
	 * @return
	 */
	public Integer matchPathPrefix(String url) {
		if(this.isFullPathPrefix) {
			return 1;
		}else {
			if(this.website.getPathPrefix().equals(url)) {
				return 4;
			}
			return 0;
		}
	}
	
	/**
	 * 匹配访问类型
	 * @param isMobile
	 * @return
	 */
	public Integer matchType(boolean isMobile) {
		if(this.isAutoType) {
			return 1;
		}else {
			if(isMobile && WebsiteEnum.Type.Mobile.index().equals(this.website.getType())) {
				return 2;
			}else if(!isMobile && WebsiteEnum.Type.PC.index().equals(this.website.getType())) {
				return 2;
			}
			return 0;
		}
	}
	
	/**
	 * 匹配语种
	 * @param lang
	 * @return 权重值，权重为0则抛弃
	 */
	public Integer matchLang(String lang) {
		if(this.website.getLang() != null && this.website.getLang().equals(lang)) {
			return 2;
		}
		return 0;
	}
	
	/**
	 * 匹配页面
	 * @param url
	 * @return
	 */
	public WebsitePage matchPage(String url) {
		if(pages != null && pages.size() > 0) {
			List<WebsitePageWrap> data = new ArrayList<WebsitePageWrap>(pages.values());
			for(WebsitePageWrap item : data) {
				if(item.matches(url)) {
					return item.getPage();
				}
			}
		}
		return null;
	}
	
	public static class WebsitePageWrap {
		
		final WebsitePage page;
		Pattern pat;
		
		public WebsitePageWrap(WebsitePage page) {
			this.page = page;
			this.pat = Pattern.compile(page.getPath());
		}
		
		public boolean matches(String url) {
			if(pat != null && StringUtils.isNotBlank(url)) {
				return pat.matcher(url).find();
			}
			return false;
		}
		
		public WebsitePage getPage() {
			return page;
		}
	}
}
